package com.hundsun.yr.universal.p2p.config;

import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.utils.MathUtils;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import de.greenrobot.event.EventBus;

/**
 * @version V1.0
 *          todo 需要解决一个问题，过于频繁的发送信息，造成的线程压力
 * @Title: Container.java
 * @Description: 存储数据容器
 * @date 2016-3-24 下午7:23:25
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com
 * @replace author:
 * @replace date:
 */
public class Container {

    public boolean isCheckVersion = false;
    private static Container mInstance;
    private boolean isFirst = true;

    public static Container getInstance() {

        if (mInstance == null) {
            mInstance = new Container();
        }
        return mInstance;

    }

    private SysTimerTask timerTask = new SysTimerTask();
    private Timer timer;
    private long serverTime;

    /**
     * 设置系统业务时间
     *
     * @param mSysTime
     */
    public void setSystemTime(String mSysTime) {

        sysTime = MathUtils.string2long(mSysTime);
        if (timer == null) {
            timer = new Timer(true);
            timer.schedule(timerTask, 0, 1000);
        }
    }

    public void setServerTime(long serverTime){
        this.serverTime = serverTime;
    }
    /**
     * 获取系统业务时间
     *
     * @return
     */
    public long getLongTime() {
        return sysTime;
    }

    /**
     * 获取系统业务时间
     *
     * @return
     */
    public String getStringTime() {
        return sysTime + "";
    }

    /**
     * 系统业务时间
     */
    private long sysTime;

    /**
     * 定义一个计时的内部类
     */
    public class SysTimerTask extends TimerTask {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            sysTime = sysTime + 1000;
            EventBus.getDefault().post(new EventCenter<HashMap<String, String>>(Extras.SYSTEM_TIME_REFRESH));
            //
//            Logs.e("serverTime: " + serverTime + "sysTime: " + sysTime);
            /*if (isFirst){
                isFirst = false;
            } else {
                if (serverTime <= sysTime){
                    timerTask.cancel();
                    timer.cancel();
                    timer = null;
                }
            }*/
/*            if (serverTime != 0 && serverTime <= sysTime){
                timerTask.cancel();
                timer.cancel();
                timer = null;
            }*/
        }
    }
}
